package com.google.code.tretris.rendering;

import javax.microedition.khronos.opengles.GL10;

public class Light implements Renderable {
	private final int GL_LIGHT_ID;
	private final float[] lightPosition = new float[4];

	public Light(final int lightID, final boolean attenuation) {
		super();
		GL_LIGHT_ID = lightID;
		final float attenuationParam;
		if (attenuation) {
			attenuationParam = 1.0f;
		} else {
			attenuationParam = 0.0f;
		}
		lightPosition[3] = attenuationParam;
	}

	public void setPosition(final float x, final float y, final float z) {
		lightPosition[0] = x;
		lightPosition[1] = y;
		lightPosition[2] = z;
	}

	/**
	 * Setup the light. Will use default values for ambient, diffuse & specular
	 * light. The light model turns of two sided normal calculations so it
	 * basically assumes gl.glEnable(GL10.GL_CULL_FACE).
	 * 
	 * @param gl
	 */
	public void initialize(final GL10 gl) {
		// Use lighting
		gl.glEnable(GL10.GL_LIGHTING);
		gl.glEnable(GL_LIGHT_ID);
		// No use lighting back faces since we cull them.
		gl.glLightModelx(GL10.GL_LIGHT_MODEL_TWO_SIDE, 0);

		float[] ambientLight = { 0.2f, 0.2f, 0.2f, 1.0f };
		gl.glLightfv(GL_LIGHT_ID, GL10.GL_AMBIENT, ambientLight, 0);

		float[] diffuseLight = { 0.8f, 0.8f, 0.8f, 1.0f };
		gl.glLightfv(GL_LIGHT_ID, GL10.GL_DIFFUSE, diffuseLight, 0);

		float[] specularLight = { 1.0f, 1.0f, 1.0f, 1.0f };
		gl.glLightfv(GL_LIGHT_ID, GL10.GL_SPECULAR, specularLight, 0);
	}

	public void render(final GL10 gl) {
		gl.glPushMatrix();
		gl.glLightfv(GL_LIGHT_ID, GL10.GL_POSITION, lightPosition, 0);
		gl.glPopMatrix();
	}

}
